import { getCateSetList } from "../../api/common";
import { aboutUser } from "../../store";

const { PlayletExtension, getPlayletManager } = tt;

// 可以使用tt.canIUse('PlayletExtension')判断是否可用，不可用可调用tt.navigateTo等跳转离开

PlayletExtension({
  pm: undefined,
  options: undefined,
  data: {
    tt_album_id: "",
    tt_episode_id: "",
    seq: 1,
    playerShow: false,
    idNo: "",
    log: "", // 日志
    logDetail: "", //详细日志
    logWithoutOnTimeUpdate: "", // 日志去除OnTimeUpdate
    logDetailWithoutOnTimeUpdate: "", //详细日志去除OnTimeUpdate
    allUnlock: {}, // 全部解锁的配置
    showOnTimeUpdate: false, // 日志是否显示OnTimeUpdate回调内容
    showDetail: false, // 日志是否显示详细内容
    setCatalogTime: 0, // 延时设置setCatalog时间,默认不延时
    playerTap: 0,
    chargeTap: 0,

    watchTime: 0, // 播放计时时长
    watchTimer: null, // 播放计时器
  },

  provide() {
    // 返回值就是要提供的数据, 初始化数据, 不会监听变动
    return {
      playerTap: this.data.playerTap,
      chargeTap: this.data.chargeTap,
    };
  },

  methods: {
    // 是否显示OnTimeUpdata日志
    showOnTimeUpdateChange(val) {
      console.log("收到消息", val);
      if (val === "player") {
        this.setProvide({
          playerTap: this.data.playerTap + 1,
        });
        this.setData({
          playerTap: this.data.playerTap + 1,
        });
      } else {
        this.setProvide({
          chargeTap: this.data.chargeTap + 1,
        });
        this.setData({
          chargeTap: this.data.chargeTap + 1,
        });
      }
    },

    onLoad(options) {
      const caniuse = !!tt.canIUse("PlayletExtension");
      console.log("caniuse", caniuse);
      // console.log("行业SDK版本号", tt.getIndustrySdkInfo().version);
      // console.log("TMAR版本号", tt.getSystemInfoSync().SDKUpdateVersion);

      const pm = getPlayletManager();
      this.pm = pm;
      this.options = options;
      this.setData({ tt_album_id: options.tt_album_id, tt_episode_id: options.tt_episode_id, seq: options.seq, isShowModal: options.isShowModal });
    },

    onUnload() {
      tt.enableUserScreenRecord();

      aboutUser.addPointFun("watchTime", this.data.watchTime);
      clearInterval(this.data.watchTimer);
      this.setData({ watchTimer: null });
    },

    async onReady() {
      let watchTimer = setInterval(() => {
        this.setData({ watchTime: ++this.data.watchTime });
      }, 1000);

      this.setData({ watchTimer });

      const options = this.options;
      console.log("页面完成Ready", JSON.stringify(options));

      //是否展示解锁页
      console.log("不展示解锁页", options.showLockPage);
      this.pm.setConfig({ showLockPage: false, objectFit: "cover" });

      this.getCateSetListFun();
    },

    async getCateSetListFun() {
      try {
        let res = await getCateSetList({ vid: this.options.vid, uid: aboutUser.getUserInfo().userId });

        if (res.code == 200) {
          this.pm.setCatalog(res.data);
        }
      } catch (e) {
        console.log("获取单部剧的解锁规则接口异常", e);
      }
    },
  },
});
